#include "MergeSort.h"
using namespace std;
MergeSort::MergeSort(long v[])
{
    for (long i = 0; i < SIZE; ++i)
        this -> array[i] = v[i];
}
void MergeSort::sort()
{
    this ->Sort_merge(0, SIZE - 1);
}
double MergeSort::getExecutionTime()
{
    clock_t start, end;
    start = clock();
    this -> sort();
    end = clock();
    return (double)(end - start) / CLOCKS_PER_SEC;
}
void MergeSort::Sort_merge(long ll, long rl)
{
	long m;
	if (ll < rl)
	{
		m = (ll + rl) / 2;
		Sort_merge(ll, m);
		Sort_merge(m + 1, rl);
		Merge(ll, m, rl);
	}
}
void MergeSort::Merge(long ll, long m, long rl)
{
	long i = ll;
	long j = m + 1;
	long k = -1;
	long c[rl - ll + 1];
	for (long x = 0; x <= rl - ll + 1; ++x)
		c[x] = 0;
	while (i <= m && j <= rl)
		if (this -> array[i] < this -> array[j])
		{
			++k;
			c[k] = this -> array[i];
			++i;
		}
        else
		{
			++k;
			c[k] = this -> array[j];
			++j;
		}
    while (i <= m)
	{
		++k;
		c[k] = this -> array[i];
        ++i;
	}
    while (j <= rl)
    {
		++k;
        c[k] = this -> array[j];
        ++j;
	}
    for (i = 0; i <= k + 1; ++i)
		this -> array[ll + i] = c[i];
}
bool MergeSort::Check()
{
    long i;
	bool  ok = true;
	for (i = 0; i < SIZE - 1; ++i)
		if (this -> array[i] > this -> array[i + 1])
		{
			ok = false;
			break;
		}
	cout << "\t" << i;
	return ok;
}

